<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript基础语法</title>
</head>

<body>

</body>
<script>
    //输入框
    // prompt("请输入数据:");
    //警告框
    // alert("Hello");
    //控制台输出
    // console.log("Hello Js");
    //页面内容输出
    // document.write("Hello Js <br>");
    // document.write("Hello Js <br>");

    //局部变量
    document.write("=====================<br>");
    let name = "貂蝉";
    let age = 30;
    document.write(name + ", " + age + "<br>");

    //全局变量
    {
        str = "Hello JavaScript";   //在代码块中定义 在外部仍然可以访问
    }
    document.write(str + "<br>");

    //常量
    const pi = 3.14;
    document.write(pi + "<br>");
    document.write("=====================<br>");

    //typeof方法 获取变量类型
    let l1 = true;
    document.write(typeof (l1) + "<br>");

    let l2 = null;
    document.write(typeof (l2) + "<br>");

    let l3;
    document.write(typeof (l3) + "<br>");

    let l4 = 10;
    document.write(typeof (l4) + "<br>");

    let l5 = "Hello";
    document.write(typeof (l5) + "<br>");

    let l6 = true;
    document.write(typeof (l6) + "<br>");

    let l7 = 1000n;
    document.write(typeof (l7) + "<br>");

    document.write("=====================<br>");

    //运算符测试
    let num = "10";     //定义一个字符串数字
    document.write(num + 5 + "<br>");   // +号拼接
    document.write(num - 5 + "<br>");   //自动转成数字相减 下同
    document.write(num * 5 + "<br>");
    document.write(num / 5 + "<br>");

    let num2 = 10;
    document.write(num == num2); //只比较数值 不比较类型
    document.write("<br>");
    document.write(num === num2); //数值和类型都比较
    document.write("<br>");

    //if while switch 语句和Java一样 省略
    document.write("=====================<br>");

    //数组定义 和 细节
    let arr = [10, 20, 30];
    arr[3] = 66;    //js中数组长度不限定死, 可任意加数据
    for (let i = 0; i < arr.length; i++) {
        document.write(arr[i] + ", ");
    }
    document.write("<br>");

    //数组复制
    let arr2 = [...arr];
    for (let i = 0; i < arr2.length; i++) {
        document.write(arr2[i] + ", ");
    }
    document.write("<br>");

    //数组合并
    let arr3 = [40, 50, 60];
    let arr4 = [...arr, ...arr3];
    for (let i = 0; i < arr4.length; i++) {
        document.write(arr4[i] + ", ");
    }
    document.write("<br>");

    //将字符串转换成数组
    let str_arr = [..."hello"];
    for (let i = 0; i < str_arr.length; i++) {
        document.write(str_arr[i] + ", ");
    }
    document.write("<br>");
    document.write("=====================<br>");
    /*
        函数
    */
    //无参无返回 函数
    function fun1() {
        document.write("Hello <br>");
    }
    fun1();
    //有参有返回
    function fun2(num1, num2) {
        return num1 + num2;
    }
    document.write("132+325=" + fun2(132, 325) + "<br>");
    //可变参数 对n个数字求和
    function fun3(...nums){
        let num=0;
        for(let i=0;i<nums.length;i++){
            num += nums[i];
        }
        return num;
    }
    document.write(fun3(12,20,30) +"<br>");

    //匿名函数
    let fun = function(){
        document.write("Hello JS");
    }
    fun();
    document.write("<br>");
    document.write("=====================<br>");




</script>

</html>